<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of VisaNetBO
 *
 * @author josem
 */
class VisaNetBO extends GeneralSisResBO {
    var $ws;
    var $wsConsulta;
    var $opcionDao;
    var $className;

    public function __construct() {
        $this->className = 'VisaNetBO';
        LogUtil::info("class $this->className", "__construct");
        $this->ws = BOFactory::getVisaNetWS();
        $this->wsConsulta = BOFactory::getVisaNetWSConsulta();
        $this->opcionDao = DAOFactory::getOpcionDao(); 
    }
    
    public function generarETicket($nroOrden, $mount, $datoComercio,$canal="3", $producto="1"){
        try{
            $VISANET_CODIGOTIENDA = $this->opcionDao->obtenerOpcionPorId(Constantes::$OPCION_CODIGO_COMERCIO_VISA);
            $xmlstr=$this->ws->generarETicket($canal, $producto, $VISANET_CODIGOTIENDA->valor, $nroOrden, $mount, $datoComercio);
            $xmlstr=utf8_decode($xmlstr);
            $xml = new SimpleXMLElement($xmlstr); 
            $mensajeXml=$xml->mensajes->mensaje;
            if(!$mensajeXml){                            
                return $this->cargarGETicketResponse($xml);                
            }else{ 
                throw new Exception(utf8_decode($mensajeXml));
            }            
         }catch(Exception $e){  
           LogUtil::info("class $this->className", $e->getMessage());
           throw new Exception($e->getMessage(), $e->getCode());
       }    
    }
    
    private function cargarGETicketResponse($xml){
        $gETicketResponse= new GETicketResponse();
        foreach($xml->registro->campo as $val){		
            $value=utf8_decode($val);
            $attr=$val->attributes();
            $attr=$attr["id"];
            if($attr=='CODTIENDA'){
                $gETicketResponse->codTienda=$value;
            }else if($attr=='NUMORDEN'){                
                $gETicketResponse->nroOrden=$value;
            }else if($attr=='ETICKET'){                    
                $gETicketResponse->eTicket=$value;
            }            
        }
        return $gETicketResponse;
    }    
    
    public function consultaETicket($codTienda,$eTicket){
        try{
            $xmlstr=$this->wsConsulta->consultaETicket($codTienda, $eTicket);
            $xmlstr=utf8_decode($xmlstr);
            $xml = new SimpleXMLElement($xmlstr);            
            $mensajeXml=$xml->mensajes->mensaje;
            if(!$mensajeXml){                   
                return $this->cargarCETicketResponse($xml);                                
            }else{                                
                throw new Exception(utf8_decode($mensajeXml));
            }            
         }catch(Exception $e){           
           LogUtil::info("class $this->className", $e->getMessage());
           throw new Exception($e->getMessage(), $e->getCode());
       }    
    }
    
    private function cargarCETicketResponse($xml){        
        $cETicketResponse= new CETicketResponse();
        foreach($xml->pedido->operacion->campo as $val){				
            $value=utf8_decode($val);
            $attr=$val->attributes();
            $attr=$attr["id"];            
            if($attr=='respuesta'){
                $cETicketResponse->respuesta=$value;
            }else if($attr=='estado'){
                $cETicketResponse->estado=$value;
            }else if($attr=='cod_tienda'){
                $cETicketResponse->codTienda=$value;
            }else if($attr=='nordent'){
                $cETicketResponse->nroOrden=$value;
            }else if($attr=='cod_accion'){
                $cETicketResponse->codAccion=$value;
            }else if($attr=='pan'){
                $cETicketResponse->pan=$value;
            }else if($attr=='nombre_th'){
                $cETicketResponse->nombreTh=$value;
            }else if($attr=='ori_tarjeta'){
                $cETicketResponse->oriTarjeta=$value;
            }else if($attr=='nom_emisor'){
                $cETicketResponse->nomEmisor=$value;
            }else if($attr=='eci'){
                $cETicketResponse->eci=$value;
            }else if($attr=='dsc_eci'){
                $cETicketResponse->dscEci=$value;
            }else if($attr=='cod_autoriza'){
                $cETicketResponse->codAutoriza=$value;
            }else if($attr=='cod_rescvv2'){
                $cETicketResponse->codRescvv2=$value;
            }else if($attr=='imp_autorizado'){
                $cETicketResponse->impAutorizado=$value;
            }else if($attr=='fechayhora_tx'){
                $cETicketResponse->fechaHoraTx=$value;
            }else if($attr=='fechayhora_deposito'){
                $cETicketResponse->fechaHoraDeposito=$value;
            }else if($attr=='fechayhora_devolucion'){
                $cETicketResponse->fechaHoraDevolucion=$value;
            }else if($attr=='dato_comercio'){
                $cETicketResponse->datoComercio=$value;
            }            
        }
        return $cETicketResponse;
    }
}

?>
